home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / common / udp_io.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-24  |  3.8 KB  |  152 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "tweak.h"
  11. #include "common_def.h"
  12.  
  13. static struct sockaddr_in INET_ZERO = { AF_INET };
  14.  
  15. extern int errno;
  16. #define DSIZE (sizeof(int)*8)
  17. #define SAVE(A) { int sav; sav = errno; A; errno = sav; }
  18.  
  19. #ifndef EXOS_IPC
  20. #include <netdb.h>
  21.  
  22. extern unsigned long inet_addr();
  23.  
  24. int _x_udp PROTO1(int *, port)
  25. {
  26.   int f, len, zz;
  27.   struct sockaddr_in me ;
  28.   struct sockaddr_in sin;
  29.   
  30.   me = sin = INET_ZERO;
  31.   
  32.   me.sin_port = htons((unsigned short) *port);
  33.   me.sin_family = AF_INET;
  34.   
  35.   if((f=socket(AF_INET,SOCK_DGRAM,0)) == -1) return(-1);
  36.   
  37.   if(setsockopt(f,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz)) < 0 ||
  38.      bind(f,(struct sockaddr *) &me,(len = sizeof(me))) < 0 ||
  39.      getsockname(f,(struct sockaddr *)&sin,&len) < 0) {
  40.     SAVE(((void) close(f)));
  41.     return(-1);
  42.   }
  43.   if(!*port) *port = ntohs((unsigned short) sin.sin_port);
  44.   return(f);
  45. }      
  46.  
  47. int _x_adr PROTO3(char *, host, int, port, struct sockaddr_in *, his)
  48. {
  49.   char myhost[128];
  50.   struct hostent *H;
  51.   int    i;
  52.   char *s, *d;
  53.   
  54.   *his = INET_ZERO;
  55.   if(!host) (void) gethostname(host = myhost,sizeof(myhost));
  56.   
  57.   if((his->sin_addr.s_addr = inet_addr(host)) != -1)
  58.     his->sin_family = AF_INET;
  59.   else
  60.     if(H = gethostbyname(host)) {
  61.       for(s = (char *)H->h_addr, d = (char *)&his->sin_addr, i = H->h_length;
  62.       i--; *d++ = *s++);
  63.       his->sin_family = H->h_addrtype;
  64.     } else return(-1);
  65.   his->sin_port = htons((unsigned short) port);
  66.   
  67.   return(0);
  68. }
  69.  
  70. int _x_select PROTO2(unsigned int *, rf, long, tt)  /* tt is in unit of ms */
  71. {
  72.   struct timeval timeout;
  73.   
  74.   if(tt != -1) {
  75.     if(tt < MIN_DELAY) tt = MIN_DELAY;
  76.     timeout.tv_sec  =  tt / 1000;
  77.     timeout.tv_usec = (tt % 1000)*1000;
  78.     return(select(DSIZE, rf, (int *) 0, (int *) 0, &timeout));
  79.   }
  80.   
  81.   return(select(DSIZE, rf, (int *) 0, (int *) 0, (struct timeval *) 0));
  82. }
  83. #endif  /* not EXOS_IPC */
  84.  
  85. #ifdef EXOS_IPC
  86.  
  87. extern long rhost();
  88.  
  89. int _x_udp PROTO1(int *, port)
  90. {
  91.   struct sockaddr_in sin; int f;
  92.   
  93.   sin = INET_ZERO;
  94.   sin.sin_family = AF_INET;
  95.   sin.sin_port   = htons((unsigned short) *port);
  96.   if((f = socket(SOCK_DGRAM, (struct sockproto *) 0, &sin,
  97.          SO_REUSEADDR)) == -1)
  98.     return(-1);
  99.   sin = INET_ZERO;
  100.   if(socketaddr(f,&sin) == -1) {
  101.     SAVE(((void) close(f)));
  102.     return(-1);
  103.   }
  104.   if(!*port) *port = ntohs((unsigned short) sin.sin_port);
  105.   return(f);
  106. }
  107.  
  108. int _x_adr PROTO3(char *, host, int, port, struct sockaddr_in *, his)
  109. {
  110.   char myhost[128];
  111.   int f;
  112.   
  113.   *his = INET_ZERO;
  114.   if(!host) (void) gethostname(host = myhost,sizeof(myhost));
  115.   
  116.   his->sin_family = AF_INET;
  117.   his->sin_port = htons((unsigned short) port);
  118.   
  119.   if((his->sin_addr.s_addr = rhost(&host)) == -1) return(-1);
  120.   
  121.   return(0);
  122. }
  123.  
  124. int _x_select PROTO2(unsigned int *, readfds, long, tt)
  125. {
  126.   int  code;
  127.   long mask = *readfds;
  128.   
  129.   if(tt & 0xc0000000) tt = 0x3fffffff;/* It does not like 0x7fffffff. */
  130.   
  131.   code = select(DSIZE, &mask, (long *) 0, tt);
  132.   
  133.   *readfds = mask;
  134.   
  135.   return(code);
  136. }
  137.  
  138. int recvfrom PROTO6(int, s, char *, msg, int, len, int, flags,
  139.             struct sockaddr_in *, from, int *, fromlen)
  140. {
  141.   return(receive(s,from,msg,len));
  142. }
  143.  
  144. int sendto PROTO6(int, s, char *, msg, int, len, int, flags,
  145.           struct sockaddr_in *, to, int *, tolen)
  146. {
  147.   to->sin_family = AF_INET;
  148.   return(send(s,to,msg,len));
  149. }
  150.  
  151. #endif /* EXOS_IPC */
  152.